<?php
namespace app\common\base;
use app\common\entity\RequestEntity;
use think\Controller;
use think\Db;
use think\Exception;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/6
 * Time: 10:23
 */
class _Db extends Controller{
    /*
     * 单表查询
     */
    public static function GetData($param_table_name, $param_field, $param_where){
        $resultset = Db::table($param_table_name)
            ->field($param_field)
            ->where($param_where)
            ->find();
        return $resultset;
    }
    public static function GetDataList($param_table_name, $param_field, $param_where){
        $resultset = Db::table($param_table_name)
            ->field($param_field)
            ->where($param_where)
            ->select();
        return $resultset;
    }
    /*
     * 日志方法
     */
    public static function log($userid,$type,$content){
        $field_log=[
            'oper_id'     => $userid,
            'oper_ip'     => self::GetIp(),
            'oper_type'   => $type,
            'oper_content'=> $content,
            'oper_sql'    => Db::table('')->getLastSql(),
            'oper_date'   => date('Y-m-d H:i:s',time()),
        ];
        Db::table('log')->insert($field_log);
    }
    /*
     * 错误代码
     */
    public static function Response($items = null, $message = ''){
        //失败
        if ($items==0) {
            $array_result['State']['Code'] = 0;
            $array_result['State']['Message'] = $message;
            exit(json_encode($array_result));
        }
        //成功
        if ($items==1) {
            $array_result['State']['Code'] = 1;
            $array_result['State']['Message'] = $message;
            exit(json_encode($array_result));
        }
        //已存在
        if ($items==2) {
            $array_result['State']['Code'] = 2;
            $array_result['State']['Message'] = $message;
            exit(json_encode($array_result));
        }
        //禁止删除
        if ($items==3) {
            $array_result['State']['Code'] = 3;
            $array_result['State']['Message'] = $message;
            exit(json_encode($array_result));
        }
    }
    /**
     * 获取单条信息
     * @param string $param_table_name 数据表名
     * @param array $param_field 待获取字段数组
     * @param array $param_where 数据查询条件数组
     * @return array
     */
    public static function GetDataInfo($param_table_name, $param_field, $param_where, $param_tag='')
    {
        try {
            $resultset_iteminfos = Db::table($param_table_name)
                ->field($param_field)
                ->where($param_where)
                ->find();
        } catch (Exception $e) {
            self::ResponseJson(null,'<p style="color:red;font-weight: bold;">Error:</p><p style="word-wrap:break-word;word-break:break-all;">'.$e->getMessage().'</p><p style="color:red;color:red;font-weight: bold;">Code:</p><p style="word-wrap:break-word;word-break:break-all;">'.$e->getCode().'</p><p style="color:red;color:red;font-weight: bold;">SQL:</p><p style="word-wrap:break-word;word-break:break-all;">'.Db::table($param_table_name)->getLastSql()).'</p>';
        }
        $json_node = explode("_", $param_table_name);
        $return_data = array();
        if($param_tag===''){
            sizeof($json_node) > 1 ?
                $param_tag = ucfirst($json_node[sizeof($json_node) - 2]) . ucfirst($json_node[sizeof($json_node) - 1]) :
                $param_tag = ucfirst($json_node[1]);
        }
        $return_data[$param_tag] = $resultset_iteminfos;
        return $return_data;
    }
    /**
     * 响应数据请求，返回Json数据
     * @param null $param_items 待返回数据数组
     * @param string $param_message 数据状态说明
     * @return string
     */
    public static function ResponseJson($param_items = null, $param_message = 'Error')
    {
        if($param_items==='token_error'){
            $array_result['State']['Code'] = -1;
            $array_result['State']['Message'] = $param_message;
            exit(json_encode($array_result));
        }

        if ($param_items===0||$param_items===false) {
            $array_result['State']['Code'] = 0;
            $array_result['State']['Message'] = $param_message;
            exit(json_encode($array_result));
        }

        if (!isset($param_items)) {
            $array_result['State']['Code'] = 0;
            $array_result['State']['Message'] = $param_message;
            exit(json_encode($array_result));
        }

        if ($param_items===1||$param_items===true) {
            $array_result['State']['Code'] = 1;
            $array_result['State']['Message'] = 'Success';
            exit(json_encode($array_result));
        }

        $array_result['State']['Code'] = 1;
        $array_result['State']['Message'] = 'Success';
        exit(json_encode(array_merge($array_result, $param_items)));
    }

    /**
     * 校验表单数据
     * @param array $param_objects 待校验数据数组
     */
    public static function Validates($param_objects)
    {
        $array_data = [];
        $array_rule = [];
        for ($i = 0; $i < sizeof($param_objects); $i++) {
            $array_data[$param_objects[$i]->name] = $param_objects[$i]->text;
            if ($param_objects[$i]->isrequire === true) {
                $array_rule[$param_objects[$i]->name] = 'require';
            }
            if ($param_objects[$i]->length > 0) {
                if (!empty($array_rule[$param_objects[$i]->name])) {
                    $array_rule[$param_objects[$i]->name] .= '|' . 'max:' . $param_objects[$i]->length;
                } else {
                    $array_rule[$param_objects[$i]->name] = 'max:' . $param_objects[$i]->length;
                }
            }
            $controller=new Controller();
            $result = $controller->validate($array_data, $array_rule);
            if (true !== $result) {
                self::ResponseJson(null, $result);
            }
        }
        return true;
    }
    /*
    * 获取用户ip地址
    */
    public static function GetIp()
    {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
            $ip = getenv("HTTP_CLIENT_IP");
        else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
            $ip = getenv("REMOTE_ADDR");
        else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
            $ip = $_SERVER['REMOTE_ADDR'];
        else
            $ip = "unknown";
        return ($ip);
    }

}